#!/bin/bash

# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BUILD_DIR=tpc_c_cplusplus/lycium
ROOT_DIR=`pwd`
COMPONT_NAME=
OHOS_SDK=$ROOT_DIR/ohos-sdk/linux
CMAKE_DIR=$ROOT_DIR/cmake-3.26.4-linux-x86_64
BUILD_LOG_NAME=
function check_prepare () {
    if [ ! -d $OHOS_SDK ]
    then
        echo "sdk not ready ##############"
        return 1
    fi
    export OHOS_SDK=$OHOS_SDK

    if [ ! -d $CMAKE_DIR ]
    then
        echo "cmake not ready ##############"
        return 1
    fi
    echo "CMAKE_DIR=$CMAKE_DIR"
    ls $CMAKE_DIR/bin
    export PATH=$CMAKE_DIR/bin:$PATH
    cmake --version

    return 0
}

function check_build_file() {
    local diff_url=$1.diff
    local diff_name=${diff_url##*/}
    local build_file=("HPKBUILD" "SHA512SUM" ".patch" ".sh")

    while read line
    do
        local tmp=`echo "$line" | grep "diff --git"`
        if [ ! -z "$tmp" ]
        then
            for file in ${build_file[@]}
            do
                tmp=`echo "$line" | grep "$file"`
                if [ ! -z "$tmp" ]
                then
                    return 1
                fi
            done
        fi
    done < $diff_name

    return 0
}

function genera_diff() {
    local diff_file=$1
    local pr_info=$ROOT_DIR/change_info.json
    if [ ! -f $pr_info ]
    then
        echo "no such file:$pr_info"
        return 1
    fi

    local json_str=`cat $pr_info`
    echo "json_str = $json_str"

    echo $json_str | jq '.tpc_c_cplusplus.changed_file_list.added' > $diff_file
    echo $json_str | jq '.tpc_c_cplusplus.changed_file_list.modified' >> $diff_file

    return 0
}

function get_thirdparty_name() {
    local diff_url=$1.diff
    local diff_name=${diff_url##*/}
    local dirs=("thirdparty" "community")
    local target_dir=

    genera_diff $diff_name
    if [ $? -ne 0 ]
    then
        echo "genera_diff $diff_name failed!"
        return 1
    fi

    while read line
    do
        local name=
        local tmp=${line%%/*}
        tmp=${tmp#\"*}
        for  dir in $dirs
        do
            if [ X"$tmp" != X"$dir" ]
            then
                continue
            fi

            if [ -z $target_dir ]
            then
                target_dir=$dir
            fi

            name=${line#*"$tmp/"}
            name=${name%%/*}
            echo "name=$name"

            if [ -z "$COMPONT_NAME" ]
            then
                COMPONT_NAME=$name
            else
                if [ $COMPONT_NAME != $name ]   # 判断一个PR是否提交多个三方库的改动
                then
                    echo "One submission only supports modification of one third-party library!"
                    return 1
                fi
            fi
        done
    done < $diff_name

    if [[ X$target_dir == "Xcommunity" ]] && [[ ! -z $COMPONT_NAME ]]
    then
        cp -arf $ROOT_DIR/tpc_c_cplusplus/Xcommunity/$COMPONT_NAME  $ROOT_DIR/tpc_c_cplusplus/thirdparty/
    fi

    return 0
}

function prepare_architecture_files() {
    if [ ! -f ${OHOS_SDK}/native/llvm/bin/aarch64-linux-ohos-clang ]
    then
        cd Buildtools
        tar -zxf toolchain.tar.gz
        if [ $? -ne 0 ]
        then
            echo "unpackage toolchain failed! #####"
        else
            cp toolchain/* ${OHOS_SDK}/native/llvm/bin
            rm -rf toolchain
        fi
        cd $OLDPWD
    fi
}

function start_build() {

    local result=0
    cd $BUILD_DIR
    prepare_architecture_files
    if [ -z "$COMPONT_NAME" ]
    then
        return 0
    fi

    if [ "$COMPONT_NAME" == "all" ]
    then
        bash build.sh > build.log
    else
        bash build.sh $COMPONT_NAME > build.log
    fi
    result=$?
    cat build.log
    echo "build result *** $result"

    if [ $result -ne 0 ]
    then
        local path_array=`find ../ | grep build.log`
        echo ""
        echo "ERROR LOS ######################"
        echo ""
        for path in ${path_array[@]}
        do
            cat $path
            echo ""
        done
    fi

    cd $ROOT_DIR

    return $result
}

function start_compile() {
    check_prepare
    if [ $? -ne 0 ]
    then
        echo "check prepare failed!"
        return 1
    fi

    get_thirdparty_name $1
    if [ $? -ne 0 ]
    then
        echo "get_thirdparty_name failed!"
        return 1
    fi

    if [ -z "$COMPONT_NAME" ]
    then
        echo "get compont_name failed! no need to compile!"
        return 0
    fi

    check_build_file $1
    if [ $? -eq 0 ]
    then
        echo "no build file changed!"
        return 0
    fi

    start_build
    if [ $? -ne 0 ]
    then
        echo "build $COMPONT_NAME failed! ######################"
        return 1
    fi

    echo "build $COMPONT_NAME SUCCESS!! ####################"
    return 0
}

start_compile $@

exit $?

#eof
